package ClientUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListModel;
import javax.swing.SwingUtilities;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import Protocol.noticeMsgWord.NoticeMsgWord_R;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NoticeJPanel extends javax.swing.JPanel {
	private JList msgList;
	private JScrollPane msgScrollPane1;
	private ClientGUI	clientGui;
	private DefaultListModel	listModel;
	
	
	class MyList extends JList {
		//DefaultListModel   model;
		public MyList(ListModel  m) {
			super(m);
			this.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
        			handleDoubleClicked(e);
        		}
			});
		}
		private void handleDoubleClicked(MouseEvent e) {
			int   buttonMask = e.getButton(),	clickCount = e.getClickCount();
    		if(buttonMask == e.BUTTON1 && clickCount == 2) {
    			
    			NoticeMsgWord_R	cmr = (NoticeMsgWord_R)msgList.getSelectedValue();
    			clientGui.mainFrame.realContactPanel.addNewNotice(cmr);
    			listModel.remove(this.getSelectedIndex());
    		}
		}
	}
	/*class MyLabel extends JLabel {
		ChatMsg_R  chatMsg;
		MyLabel(String name, ChatMsg_R  cmr) {
			super(name);
			chatMsg = cmr;
		}
		public ChatMsg_R getChatMsg() {
			return chatMsg;
		}
	}*/
	
	public NoticeJPanel(ClientGUI c) {
		super();
		clientGui = c;
		listModel = new DefaultListModel();
		/*listModel.add(0,"test");
		listModel.add(1,"test");*/
		initGUI();
	}
	public void addNewMsg(NoticeMsgWord_R  cmr) {
		
		SwingUtilities.invokeLater(new MyRunnable(cmr));
		
	}
	private class MyRunnable implements Runnable {
		NoticeMsgWord_R   chatMsg_R;
		
		MyRunnable(NoticeMsgWord_R cmr) {
			this.chatMsg_R = cmr;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			//msgList.add(new MyLabel("new message", chatMsg_R), -1);
			listModel.add(listModel.getSize(), chatMsg_R);
			msgList.repaint();
			clientGui.mainFrame.tipPanel.repaint();
			clientGui.mainFrame.repaint();
		}
		
	}	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(300, 300));
			{
				msgScrollPane1 = new JScrollPane();
				this.add(msgScrollPane1, BorderLayout.CENTER);
				msgScrollPane1.setForeground(new java.awt.Color(128,255,0));
				msgScrollPane1.setBackground(new java.awt.Color(255,255,0));
				//msgScrollPane1.setBounds(this.getWidth() / 4, this.getHeight() / 6, this.getWidth() / 2, this.getHeight() * 2 / 3);
				{
					msgList = new MyList(listModel);
					msgList.setBackground(Color.CYAN);
					msgScrollPane1.setViewportView(msgList);
					//msgList.setBounds(67, 40, 272, 211);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
